package com.dhl.fin.api.service;

import cn.hutool.core.collection.CollectionUtil;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.dao.MyFamilyConfigDao;
import com.dhl.fin.api.domain.Tag;
import com.dhl.fin.api.dto.TagGroupDTO;
import com.dhl.fin.api.enums.BusinessTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TagService extends CommonService<Tag> {

    @Autowired
    private MyFamilyConfigDao tagDao;

    public List<TagGroupDTO> selectTagGroup(String familyUnicode) {
        List<TagGroupDTO> tags = tagDao.selectTagGroup(familyUnicode);
        if (CollectionUtil.isNotEmpty(tags)) {
            tags = tags.stream()
                    .peek(tag -> tag.setSort(BusinessTypeEnum.getByName(tag.getType()).getCode()))
                    .sorted(Comparator.comparing(TagGroupDTO::getSort))
                    .collect(Collectors.toList());
        }
        return tags;
    }

}
